######################################
# Generate plots using conjoint data
######################################

rm(list=ls())

library("ggplot2")
library(gridExtra)

sink("~/Dropbox/Atacama 2015/10_replication/003_conjoint_plots.txt")

# set working directory
setwd("~/Dropbox/Atacama 2015/10_replication")

########################
# Control
########################

d <- read.table("center_nonexposedgroup.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2,3,4),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Gender    <- paste(c("1b",2),".atgender",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")
Experience       <- paste(c("1b",2,3),".atexperience",sep="")
Expectations       <- paste(c("1b",2),".atexpectations",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Gender]       <- "Gender"
  d$gruppe[d$var %in% Age]          <- "Age"
  d$gruppe[d$var %in% Experience]   <- "Experience"
  d$gruppe[d$var %in% Expectations] <- "Expectations"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Gender,],
             d[d$var %in% Age,],
             d[d$var %in% Experience,],
             d[d$var %in% Expectations,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Center",
                                               "Right",
                                               "Left",
                                               "Independent"))
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Professor",
                                                 "Engineer"))
  
  d$var[d$var %in% Gender] <- paste(offset,c("Male",
                                             "Female"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  d$var[d$var %in% Experience] <-paste(offset,c("No experience",
                                                "Council member",
                                                "Mayor"))
  
  d$var[d$var %in% Expectations] <- paste(offset,c("Will NOT distribute a financial relief",
                                                   "Will distribute a financial relief"))
  
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "GENDER:",
                        "   ",
                        "AGE:",
                        "    ",
                        "EXPERIENCE:",
                        "     ",
                        "EXPECTATIONS:"
),order=c(.5,4.1,4.5,7.1,7.5,9.1,9.5,12.1,12.5,15.1,15.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])
d = d[2:5,]

yylab  <- c("")

f_center_control = ggplot(d,aes(y=pe,x=var)) + geom_hline(yintercept=0, size=1.5, color = "gray") + labs(title = "Control group") + coord_flip(ylim = c(-.4, .4)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=2,position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text.y=element_text(size=44)) + theme(plot.title = element_text(hjust = 0.5, size = 44)) + theme(text = element_text(size=30))
f_center_control

########################
# Exposed group
########################

d <- read.table("center_exposedgroup.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2,3,4),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Gender    <- paste(c("1b",2),".atgender",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")
Experience       <- paste(c("1b",2,3),".atexperience",sep="")
Expectations       <- paste(c("1b",2),".atexpectations",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Gender]       <- "Gender"
  d$gruppe[d$var %in% Age]          <- "Age"
  d$gruppe[d$var %in% Experience]   <- "Experience"
  d$gruppe[d$var %in% Expectations] <- "Expectations"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Gender,],
             d[d$var %in% Age,],
             d[d$var %in% Experience,],
             d[d$var %in% Expectations,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Center",
                                               "Right",
                                               "Left",
                                               "Independent"))
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Professor",
                                                 "Engineer"))
  
  d$var[d$var %in% Gender] <- paste(offset,c("Male",
                                             "Female"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  d$var[d$var %in% Experience] <-paste(offset,c("No experience",
                                                "Council member",
                                                "Mayor"))
  
  d$var[d$var %in% Expectations] <- paste(offset,c("Will NOT distribute a financial relief",
                                                   "Will distribute a financial relief"))
  
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "GENDER:",
                        "   ",
                        "AGE:",
                        "    ",
                        "EXPERIENCE:",
                        "     ",
                        "EXPECTATIONS:"
),order=c(.5,4.1,4.5,7.1,7.5,9.1,9.5,12.1,12.5,15.1,15.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])
d = d[2:5,]

yylab  <- c("")

f_center_exposed = ggplot(d,aes(y=pe,x=var)) + geom_hline(yintercept=0, size=1.5, color = "gray") + labs(title = "Exposed group") + coord_flip(ylim = c(-.4, .4)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=2,position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text.y=element_text(size=44)) + theme(plot.title = element_text(hjust = 0.5, size = 44)) + theme(axis.text.x=element_text(size=44)) + theme(text = element_text(size=30))
f_center_exposed

######################################
# Difference plots
######################################

# CENTER

d <- read.table("center_differenceplot.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2,3,4),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Gender    <- paste(c("1b",2),".atgender",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")
Experience       <- paste(c("1b",2,3),".atexperience",sep="")
Expectations       <- paste(c("1b",2),".atexpectations",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Gender]       <- "Gender"
  d$gruppe[d$var %in% Age]          <- "Age"
  d$gruppe[d$var %in% Experience]   <- "Experience"
  d$gruppe[d$var %in% Expectations] <- "Expectations"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Gender,],
             d[d$var %in% Age,],
             d[d$var %in% Experience,],
             d[d$var %in% Expectations,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Center",
                                               "Right",
                                               "Left",
                                               "Independent"))
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Professor",
                                                 "Engineer"))
  
  d$var[d$var %in% Gender] <- paste(offset,c("Male",
                                             "Female"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  d$var[d$var %in% Experience] <-paste(offset,c("No experience",
                                                "Council member",
                                                "Mayor"))
  
  d$var[d$var %in% Expectations] <- paste(offset,c("Will NOT distribute a financial relief",
                                                   "Will distribute a financial relief"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "GENDER:",
                        "   ",
                        "AGE:",
                        "    ",
                        "EXPERIENCE:",
                        "     ",
                        "EXPECTATIONS:"
),order=c(.5,4.1,4.5,7.1,7.5,9.1,9.5,12.1,12.5,15.1,15.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])
d = d[2:5,]

yylab  <- c("")

f_center_diff = ggplot(d,aes(y=pe,x=var)) + geom_hline(yintercept=0, size=1.5, color = "gray", linetype = 1)  + labs(title = "Difference") + coord_flip(ylim = c(-.4, .4)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=2,position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text.y=element_text(size=44)) + theme(plot.title = element_text(hjust = 0.5, size = 44)) + theme(axis.text.x=element_text(size=44)) + theme(text = element_text(size=30))

#############################
# Multiple plot function
#############################

pdf('~/Dropbox/Atacama 2015/10_replication/main_plot.pdf',width = 50,height=20)
grid.arrange(cbind(ggplotGrob(f_center_exposed), ggplotGrob(f_center_control), ggplotGrob(f_center_diff), size="last"))
dev.off()

sink()
